﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CVCommunity.Provider;
using CVCommunity.Kernel;
using CVCommunity.Kernel.DALFactory;
using CVCommunity.Kernel.Handlers;
using CVCommunity.Kernel.Entity;
using System.Xml;
using System.Text;

    public partial class login_iframe : CVCommunity.Kernel.BasePage
    {
        CVCommunity.Kernel.BasePage BP = new BasePage();      
         protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["action"] == null)
            {
                if (Session["UID"] == null)
                {
                    login.Style.Value = "display:block;";
                    logout.Style.Value = "display:none;";
                }
                if (user.IsGuest)
                {
                    login.Style.Value = "display:block;";
                    logout.Style.Value = "display:none;";
                }
                else
                {
                    UN.Text = Server.UrlDecode(CookieProvider.Get("user").Values["UN"]);
                    CRD0.Text = CookieProvider.Get("user").Values["CRD0"];
                    CRD1.Text = CookieProvider.Get("user").Values["CRD1"];
                    LastTime.Text = Convert.ToString(UserHandler.GetMember(user.UID, conn).LastTime);
                    login.Style.Value = "display:none;";
                    logout.Style.Value = "display:block;";
                }
            }
            else if (Request.QueryString["action"] == "logout")
            {
                UserHandler.Logout(user, conn);
                Session["UID"] = null;
                Session["PWD"] = null;
                Session["nFlag"] = null;
            }
            else if (Request.QueryString["action"].ToString() == "login" && Request.QueryString.Count == 4)
            {
                Response.ContentType = "text/xml";
                XmlTextWriter writer = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
                writer.Formatting = Formatting.Indented;
                writer.Indentation = 4;
                writer.WriteStartDocument();
                writer.WriteStartElement("Xml");

                string PSW="";
                string UID = Server.UrlDecode(Request.QueryString["postuid"]);
                try
                {
                    PSW = UserHandler.GetMember(UID, conn).Password;
                }
                catch (Exception ex)
                {
                    writer.WriteElementString("ErrorMsg", "无此账号ID!");
                    writer.WriteEndElement();
                    writer.Flush();
                    writer.Close();
                    Response.End();
                }

                if (PSW == System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Request.QueryString["postpwd"].ToString(), "MD5"))
                {
                    writer.WriteElementString("CRD0", UserHandler.GetMember(UID, conn).Credits[0].ToString());
                    writer.WriteElementString("CRD1", UserHandler.GetMember(UID, conn).Credits[1].ToString());
                    writer.WriteElementString("UN", UserHandler.GetMember(UID, conn).Username);
                    writer.WriteElementString("LastTime", UserHandler.GetMember(UID, conn).LastTime.ToString());
                    writer.WriteElementString("ErrorMsg", "OK");
                    writer.WriteEndElement();
                    writer.Flush();
                    writer.Close();
                    Session["UID"] = UserHandler.GetMember(UID, conn).UID;
                    Session["UserName"] = UserHandler.GetMember(UID, conn).Username;
                    Session["PWD"] = UserHandler.GetMember(UID, conn).Password;
                    Session["nFlag"] = "HongQiao@#JJ@#@";
                    int SaveDays;
                    if (Request.QueryString["posturm"].ToString() == "false")
                    {
                        SaveDays = -1;
                    }
                    else
                    {
                        SaveDays = 365;
                    }

                    UserHandler.CheckLogin(false, Request.QueryString["postuid"].ToString(), Text.MD5(Request.QueryString["postpwd"].ToString()), "", "", SaveDays, "default", 1, false, conn);
                    conn.Close();
                    Response.End();
                }
                else
                {
                    writer.WriteElementString("ErrorMsg", "用户名或者密码错误!");
                    writer.WriteEndElement();
                    writer.Flush();
                    writer.Close();
                    Response.End();
                }
            }
        }
    }